package com.mbyte.easy.listener;

import com.mbyte.easy.admin.entity.Draft;
import com.mbyte.easy.event.ChangeDraftEvent;
import com.mbyte.easy.support.es.helper.EsDraftHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @ClassName: RegisterEmailListener
 * @Author: lxt
 * @Description: 监听用户注册，发送邮件
 * @Version: 1.0
 */
@Slf4j
@Component
public class SyncDraft2EsListener implements ApplicationListener<ChangeDraftEvent> {

    @Autowired
    private EsDraftHelper esDraftHelper;
    /**
     * 监听稿件变化事件
     * 异步执行
     * @param event
     */
    @Async
    @Override
    public void onApplicationEvent(ChangeDraftEvent event) {
        Draft draft = (Draft) event.getSource();
        log.info("开始同步稿件数据到es=>{}",draft);
        boolean result = esDraftHelper.sync(draft);
        log.info("结束同步稿件数据到es，结果=>{}",result);
    }
}
